import Vue from 'vue'
import VueRouter from 'vue-router'
import 'nprogress/nprogress.css'
/* Layout */
import Layout from '@/layout'

// 解决 `element ui` 导航栏重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

// 安装 VueRouter 插件
Vue.use(VueRouter)

//
// /**
//  * Note: 路由配置项
//  *
//  * redirect: noRedirect           // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
//  * name:'router-name'             // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
//  * meta : {
//     requireLogin: false          // 如果设置为false，则不会需要登录就可以访问
//     isKeepAlive: true            // 如果设置为true，则会被 <keep-alive> 缓存(默认 false)
//     title: 'title'               // 设置该路由在侧边栏和面包屑中展示的名字
//     isHide: true                 // 是否显示在菜单等
//     isAffix: true                 // 是否固定tagView
//     icon: 'svg-name'             // 设置该路由的图标，iconfont图标
//   }
//  */
// 定义静态路由
export const staticRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: 'index',
    children: [
      {
        path: '/index',
        component: (resolve) => require(['@/views/dashboard/index'], resolve),
        name: 'Dashboard',
        meta: {title: '首页', icon: 'iconfont icon-shouye ', isHide: false, isKeepAlive: true, isAffix: true}
      },
      {
        path: '/user/profile',
        component: (resolve) => require(['@/views/system/user/profile/index'], resolve),
        name: 'Profile',
        meta: {title: '个人中心', icon: 'icon-gerenzhongxin iconfont'}
      },
      {
        path: '/auth/role/:userId(\\d+)',
        component: (resolve) => require(['@/views/system/user/authRole'], resolve),
        name: 'AuthRole',
        meta: {title: '分配角色', icon: 'user', isHide: true}
      },
      {
        path: '/dept/post',
        component: (resolve) => require(['@/views/system/dept/post'], resolve),
        name: 'DeptPost',
        meta: {title: '部门岗位', icon: 'user', isHide: true, isKeepAlive: false}
      },
      {
        path: '/dict/type/data/:dictId(\\d+)',
        component: (resolve) => require(['@/views/system/dict/data'], resolve),
        name: 'DictData',
        meta: {title: '字典数据', icon: 'user', isHide: true}
      },
      {
        path: '/gen/edit/:tableId(\\d+)',
        component: (resolve) => require(['@/views/tool/gen/editTable'], resolve),
        name: 'GenEdit',
        meta: {title: '修改生成配置'}
      },
      {
        path: '/workflow/task/record/start',
        component: (resolve) => require(['@/views/workflow/task/process/start'], resolve),
        name: 'workflowStart',
        meta: {title: '流程发起', icon: ''}
      },
      {
        path: '/workflow/task/record/handle',
        component: (resolve) => require(['@/views/workflow/task/process/handle'], resolve),
        name: 'workflowHandle',
        meta: {title: '流程发起', icon: ''}
      }]
  },
  {
    path: '/test',
    component: (resolve) => require(['@/views/test/index'], resolve),
    name: 'TestView',
    meta: {title: '测试页', icon: 'iconfont icon-shouye ', isHide: false, isKeepAlive: true, isAffix: false}
  },
  {
    path: '/ops',
    component: (resolve) => require(['@/views/system/ops/index.vue'], resolve),
    name: '维护工具',
    meta: {title: '维护工具', icon: 'iconfont icon-shouye ', isHide: false, isKeepAlive: false, isAffix: false}
  },
  {
    path: '/redirect',
    component: Layout,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: (resolve) => require(['@/views/redirect'], resolve)
      }
    ]
  },
  {
    path: '/sample',
    component: (resolve) => require(['@/views/sampleManagement/sample'], resolve),
  },
  {
    path: '/login',
    component: (resolve) => require(['@/views/login/index'], resolve),
    meta: {requireLogin: false, title: '登录'}
  },
  {
    path: '/register',
    component: (resolve) => require(['@/views/register'], resolve),
    meta: {requireLogin: false, title: '注册'}
  },
  {
    path: '/404',
    component: (resolve) => require(['@/views/error/404'], resolve)
  },
  {
    path: '/401',
    component: (resolve) => require(['@/views/error/401'], resolve)
  }
]

/**
 * 因为路由有优先级的概念，先定义的会先匹配，而自动注册是依据文件名的排序来遍历的
 * 所以下面这种情况，如果访问 /news/edit ，会指向到 info.vue 页面上
 * a.js   /news/:id   info.vue
 * b.js   /news/edit   edit.vue
 * 为避免这种情况发生，同一模块下的路由必须放在一个路由配置文件里
 * 按上面的例子，news 模块的路由，应该放到一个类似于 news.js 的文件里
 * 至于模块里的路由优先级，可以把 /news/edit 放在 /news/:id 前面，或者把 /news/:id 改成 /news/info/:id 均可
 */
const require_module = require.context('./modules', false, /.js$/)
require_module.keys().forEach((file_name) => {
  staticRoutes.push(require_module(file_name).default)
})
function getAbsolutePath () {
  let path = window.location.pathname
  return path.substring(0, path.lastIndexOf('/') + 1)
 }
// 加载静态路由
const createRouter = () =>
  new VueRouter({
    mode: 'history', // 去掉url中的#
    base:'/wj/',
    // base:getAbsolutePath(),
    // base:'./',
    routes: staticRoutes.flat()
  })


// 重置路由
export function resetRouter() {
  router.matcher = createRouter().matcher
}

// 创建路由
const router = createRouter()


// 导出路由
export default router
